package com.jerei.developer.service.impl;

import com.jerei.developer.service.ISingleTableGenerateService;
import com.jerei.developer.utils.UtilsRoot;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Created by sunfeihu on 2016/10/19.
 * 单表生成工具
 */
@Service
public class MySqlSingleTableGenerateServiceImpl implements ISingleTableGenerateService {

    @Autowired
    private UtilsRoot utilsRoot;

    private String pluginId;

    @Override
    public void gEntity(String tableName) {
        String folder = pluginId + File.separator + "src" + File.separator + "main";
        folder += File.separator + "java";
        folder += File.separator + "com";
        folder += File.separator + "jerei";
        folder += File.separator + "plugin";
        folder += File.separator + "entity";
        utilsRoot.templateOut("Entity.ftl", "", tableName, folder, this.pluginId);
    }


    @Override
    public void gControl(String tableName) {
        String folder = pluginId + File.separator + "src" + File.separator + "main";
        folder += File.separator + "java";
        folder += File.separator + "com";
        folder += File.separator + "jerei";
        folder += File.separator + "plugin";
        folder += File.separator + "control";
        utilsRoot.templateOut("Control.ftl", "Control", tableName, folder, this.pluginId);
    }

    /**
     * 通用生成，用于生成Service和Dao
     *
     * @param tableName 表面
     * @param layerName 三层的层名，Service和Dao
     */
    private void gCommon(String tableName, String layerName, String folder) {
        //生成接口
        utilsRoot.templateOut("I" + layerName + ".ftl", layerName, tableName, folder, this.pluginId);
        //生成实现
        utilsRoot.templateOut(layerName + "Impl.ftl", layerName + "Impl", tableName, folder + File.separator + "impl", this.pluginId);
    }

    @Override
    public void gService(String tableName) {
        String folder = pluginId + File.separator + "src" + File.separator + "main";
        folder += File.separator + "java";
        folder += File.separator + "com";
        folder += File.separator + "jerei";
        folder += File.separator + "plugin";
        folder += File.separator + "service";
        this.gCommon(tableName, "Service", folder);
    }

    @Override
    public void gDao(String tableName) {
        String folder = pluginId + File.separator + "src" + File.separator + "main";
        folder += File.separator + "java";
        folder += File.separator + "com";
        folder += File.separator + "jerei";
        folder += File.separator + "plugin";
        folder += File.separator + "dao";
        this.gCommon(tableName, "Dao", folder);
    }

    @Override
    public void gList(String tableName) {

    }

    @Override
    public void gEdit(String tableName) {

    }

    @Override
    public void gView(String tableName) {

    }

    private void gPom(String basePath) {
        Map<String, Object> root = new HashMap<String, Object>();
        root.put("pluginId", this.pluginId);
        //生成pom文件
        utilsRoot.templateOut("plugins/pom.ftl", basePath + File.separator + "pom.xml", root, this.pluginId);
    }

    private void gTest(String basePath) {
        //生成test文件夹
        String folder = basePath + File.separator + "src" + File.separator + "test";
        folder += File.separator + "java";
        File f = new File(folder);
        if (!f.exists()) {
            f.mkdirs();
        }
    }

    private void gAssembly(String basePath) {
        String folder = basePath + File.separator + "src" + File.separator + "main";
        folder += File.separator + "assembly";
        File f = new File(folder);
        if (!f.exists()) {
            f.mkdirs();
        }
        Map<String, Object> root = new HashMap<String, Object>();
        root.put("pluginId", this.pluginId);
        //生成assembly文件
        utilsRoot.templateOut("plugins/assembly.ftl", f.getAbsolutePath() + File.separator + "assembly.xml", root, this.pluginId);
    }

    private void gResources(String basePath) {
        String folder = basePath + File.separator + "src" + File.separator + "main";
        folder += File.separator + "resources";
        File f = new File(folder);
        if (!f.exists()) {
            f.mkdirs();
        }
        Map<String, Object> root = new HashMap<String, Object>();
        root.put("pluginId", this.pluginId);
        //生成plugin.properties文件
        utilsRoot.templateOut("plugins/properties.ftl", f.getAbsolutePath() + File.separator + "plugin.properties", root, this.pluginId);
        //生成application文件
        utilsRoot.templateOut("plugins/application.ftl", f.getAbsolutePath() + File.separator + "application-" + this.pluginId + ".xml", root, this.pluginId);
    }

    private void gI18n(String basePath) {
        String folder = basePath + File.separator + "src" + File.separator + "main";
        folder += File.separator + "resources";
        folder += File.separator + "i18n";
        String[] arr = new String[]{"de", "en", "es", "fr", "it", "ko", "pt", "tw", "zh"};
        String[] arr1 = new String[]{"DE", "US", "ES", "FR", "IT", "KR", "BR", "TW", "CN"};
        for (int i = 0; i < arr.length; i++) {
            String cFolder = folder + File.separator + arr[i];
            File fo = new File(cFolder);
            if (!fo.exists()) {
                fo.mkdirs();
            }
            File f = new File(cFolder + File.separator + "1_" + arr[i] + "_" + arr1[i] + ".properties");
            try {
                f.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void gAll(String tableName, String pluginId) {
        this.pluginId = pluginId;
        String basePath = this.getClass().getClassLoader().getResource("").getFile() + File.separator + "output" + File.separator + pluginId;
        //生成test文件夹
        this.gTest(basePath);
        //生成pom文件
        this.gPom(basePath);
        //生成assembly
        this.gAssembly(basePath);
        //生成资源文件
        this.gResources(basePath);
        //生成i18n文件
        this.gI18n(basePath);
        this.gEntity(tableName);
        this.gDao(tableName);
        this.gService(tableName);
        this.gControl(tableName);
        this.gEdit(tableName);
        this.gList(tableName);
    }
}